package com.hundsun.monitor.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.hundsun.monitor.client.gadgets.CalendarGadget;
import com.hundsun.monitor.client.gadgets.HelloWorldGadget;
import com.hundsun.monitor.client.gadgets.ToDoListGadget;
import com.hundsun.monitor.client.model.GadgetClass;
import com.hundsun.monitor.client.view.DesktopView;
import com.hundsun.monitor.client.view.DesktopViewListener;

public class MyHome implements EntryPoint, DesktopViewListener
{
	Storage storage;
	static DesktopView view;
	public void onModuleLoad() {
		//add gadgets
		GadgetClass.addClass( new HelloWorldGadget.Class() );
		GadgetClass.addClass( new CalendarGadget.Class() );
		GadgetClass.addClass( new ToDoListGadget.Class() );
//		GadgetClass.addClass( new GoogleMapsGadget.Class() );
//		GadgetClass.addClass( new WeatherGadget.Class() );
//		GadgetClass.addClass( new NewsGadget.Class() );
//		GadgetClass.addClass( new DiceWarsGadget.Class() );
		
		//create view
		view = new DesktopView(this);
		RootPanel.get("DesktopView").add( view ); 
		
		//load layout from storage
//		if( GearsStorage.isGearsInstalled() )
//			storage = new GearsStorage();
//		else
			storage = new CookieStorage();
		String layout = storage.getValue( "layout" );
		if( layout != null )
			view.setLayoutFromString( layout );
	}
	
	public void onInterfaceChange()
	{
		//save layout to storage
		String layout = view.getLayoutAsString();
		storage.setValue( "layout", layout );
		try{
			storage.save();
		}catch( StorageException e ){
			Window.alert("unable to save the desktops state");
		}
	}

	public static DesktopView getView() {
		return view;
	}
	
}